﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;
using Exam.Core.DBUtility;
using System.Data;
using System.Collections;
using System.Data.OleDb;

namespace Exam.Core.DAL {
    public class Knowledge {

        internal List<Exam.Core.Model.Knowledge> getKnowledgeList(string courseid) {
            List<Exam.Core.Model.Knowledge> list = new List<Exam.Core.Model.Knowledge>();
            string SQL = "SELECT * FROM Knowledge WHERE CourseId=" + courseid;
            DataTable dt = DbHelperOleDb.Query(SQL).Tables[0];
            foreach (DataRow dr in dt.Rows) {
                Exam.Core.Model.Knowledge obj = new Exam.Core.Model.Knowledge();

                obj.Id = dr["KnowledgeId"].ToString().Trim();
                obj.Title = dr["KnowledgeTitle"].ToString().Trim();
                obj.Parent = courseid;
                obj.QuestionList = null;//new Question().getQuestionByKnowledgeId(obj.Id);//知识点下试题
                obj.QuestionCount = new Question().getCountByKnowledgeId(obj.Id, courseid); //知识点下试题数量
                
                list.Add(obj);
            }
            //可以加入缓存
            return list;
        }


        public bool Add(string reqStr, int cid) {
            bool result = false;
            string[] str = reqStr.Split(',');
            ArrayList SQLArrayList = new ArrayList();
            foreach (string s in str) {
                if (s.Trim() != "") {
                    Hashtable SQLHasList = new Hashtable();
                    string sql = "INSERT INTO Knowledge (KnowledgeTitle,CourseId) VALUES (@KnowledgeTitle,@CourseId)";
                    OleDbParameter[] parms = new OleDbParameter[] { 
                        new OleDbParameter("@KnowledgeTitle",OleDbType.VarChar,255),
                        new OleDbParameter("@CourseId",OleDbType.Integer,4)
                    };
                    parms[0].Value = s.Trim();
                    parms[1].Value = cid;
                    SQLHasList.Add(sql, parms);
                    SQLArrayList.Add(SQLHasList);
                }

            }
            try {
                DbHelperOleDb.ExecuteSqlTranByArrayList(SQLArrayList);
                result = true;
            }
            catch {
                result = false;
            }
            return result;
        }

        public bool Del(int id) {
            bool result = false;
            //删除知识点下试题
            string sql = "DELETE FROM Question WHERE QuestionKnowledgeId in(SELECT KnowledgeId FROM Knowledge WHERE KnowledgeId=" + id + ")";
            if (DbHelperOleDb.ExecuteSql(sql) >= 0) {
                //删除试题
                sql = "DELETE FROM Knowledge WHERE KnowledgeId=" + id;
                if (DbHelperOleDb.ExecuteSql(sql) > 0) {
                    result = true;
                }
            }
            return result;
        }
        public int ExecuteSqlTranByList(List<String> SQLStringList)
        {
            int result = 0;
            try
            {
                result = DbHelperOleDb.ExecuteSqlTranByList(SQLStringList);
            }
            catch
            {
                result = 0;
            }
            return result;
        }
    }
}
